<?php
include('../controller/functions.php');
User::connection();

$topicid = '52';

if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 350Kb
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
    ($_FILES["uploaded_file"]["size"] < 2000000)) {
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/topic_img/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place 
			//if(($old != NULL)&&($old != 'defaultdp.png')){
			//$do = unlink("profile_img/$old");} //delete the current photo 
			$up_query = mysql_query("UPDATE `tblfrtopic` SET topicimg='$filename' WHERE topicid='$topicid'");//update avatar filename
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))&&($up_query)) {
    //image upload on forums accepts .bmp, .jpg, .jpeg, and .gif that limits the updload upto 1MB.
	
	$data = mysql_query("SELECT * from `tblfrtopic` where topicid='$topicid'"); //where condition
	while($row = mysql_fetch_array($data)){	

	echo "<p style='text-align:center;'>You have successfully changed your picture!</p>
	<table width='126px' height='86' border='0' align='center' cellpadding='5' cellspacing='0'>
  <tr>
    <td>
	<div class='img_h'>
		<img src='topic_img/"."$row[15]"."' width='125px' />
		<br/><center>Current Picture</center>
	</div>
	</td>
  </tr>
</table>
";
	  
        } }else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
      }
  } else {
     echo "Error: Only .jpg images under 2MB are accepted for upload";
  }
} else {
 echo "Error: No file uploaded";
}
	//}
	

	


?>